package com.uduew.sjd.view.xpop;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.uduew.sjd.R;
import com.uduew.sjd.bean.CustomerBean;
import com.kongzue.dialogx.dialogs.PopTip;
import com.lxj.xpopup.core.CenterPopupView;
import com.uduew.utils.ActivityUtils;
import com.uduew.utils.ClipboardUtils;

public class CustomerXpop extends CenterPopupView implements View.OnClickListener {
    TextView phoneNumberTv, emailNumber, whatsAppNumberTv, lineNumberTv;
    CustomerBean customerBean;
    private View phoneNumberll, emailNumberll, whatsAppNumberll, lineNumberll;

    public CustomerXpop(@NonNull Context context, CustomerBean customerBean) {
        super(context);
        this.customerBean = customerBean;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.xpop_customer;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        phoneNumberll = findViewById(R.id.phone_number_ll);
        emailNumberll = findViewById(R.id.email_number_ll);
        whatsAppNumberll = findViewById(R.id.whatsApp_number_ll);
        lineNumberll = findViewById(R.id.line_number_ll);

        phoneNumberTv = findViewById(R.id.phone_number_tv);
        phoneNumberTv.setText(customerBean.getPhoneNumber());
        emailNumber = findViewById(R.id.email_number_tv);
        emailNumber.setText(customerBean.getEmailNumber());
        whatsAppNumberTv = findViewById(R.id.whatsApp_number_tv);
        whatsAppNumberTv.setText(customerBean.getWhatsAppNumber());
        lineNumberTv = findViewById(R.id.line_number_tv);
        lineNumberTv.setText(customerBean.getLineNumber());
        findViewById(R.id.phone_number_ll).setOnClickListener(this);
        findViewById(R.id.email_number_ll).setOnClickListener(this);
        findViewById(R.id.whatsApp_number_ll).setOnClickListener(this);
        findViewById(R.id.line_number_ll).setOnClickListener(this);
    }

    private void copyText(String text) {
        ClipboardUtils.copyText(text);
        PopTip.show(getContext().getString(R.string.copy_successful)).iconSuccess();
        dismiss();
    }

    private boolean isWhatsAppInstalled() {
        PackageManager pm = getContext().getPackageManager();
        try {
            pm.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

    private void gotoWebWhatsApp(String phoneNumber) {
        // WhatsApp未安装，引导用户安装或使用网页版
        Uri webUri = Uri.parse("https://web.whatsapp.com/send?phone=" + phoneNumber);
        Intent webIntent = new Intent(Intent.ACTION_VIEW, webUri);
        ActivityUtils.getTopActivity().startActivity(webIntent);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.phone_number_ll) {
//            copyText(customerBean.getPhoneNumber());
            //调用打电话
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:" + customerBean.getPhoneNumber()));
            try {
                ActivityUtils.getTopActivity().startActivity(intent);
            } catch (ActivityNotFoundException e) {
                PopTip.show("未安装电话").iconError();
            }
        } else if (v.getId() == R.id.email_number_ll) {
            copyText(customerBean.getEmailNumber());
        } else if (v.getId() == R.id.whatsApp_number_ll) {
            dismiss(); // 关闭弹窗
            if (!isWhatsAppInstalled()) {
                PopTip.show("未安装 whatsapp").iconError();
//                gotoWebWhatsApp(customerBean.getWhatsAppNumber());
                return;
            }
            try {
                String phoneNumber = customerBean.getWhatsAppNumber();
                String url = "https://wa.me/" + phoneNumber;
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                // 可选：限制仅由WhatsApp处理
                intent.setPackage("com.whatsapp");
                try {
                    ActivityUtils.getTopActivity().startActivity(intent);
                } catch (ActivityNotFoundException e) {
                    gotoWebWhatsApp(customerBean.getWhatsAppNumber());
                }
            } catch (ActivityNotFoundException e) {
                PopTip.show("未安装 WhatsApp").iconError();
            }
        } else if (v.getId() == R.id.line_number_ll) {
            copyText(customerBean.getLineNumber());
        }
    }
}